package com.forestar.xzk.file;

import android.content.Context;
import android.os.Build;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * created by  xzk
 * 2020/7/23   15:59
 * 说明:
 *
 */
public class AssertsUtils {
//TODO Administrator 2020/7/23  尽量多分析一些asserts的使用场景

    /**
     *Administrator
     *2020/7/23
     * getStrFromAsserts
     * 获取asserts文件中的内容
     **/
    public static String getStrFromAsserts(String absolutPath, Context activity){
        InputStream open = null;
        try {
            open = activity.getAssets().open(absolutPath);
            StringBuilder sbr = new StringBuilder();
            int available = open.available();
            byte[] bytes = new byte[available];
            open.read(bytes);
            Charset utf8 = Charset.forName("UTF-8");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                utf8 = StandardCharsets.UTF_8;
            }
            sbr.append(new String(bytes, utf8).replaceAll("\r", "")
                    .replaceAll("\n", "").replaceAll(" ", ""));
            return sbr.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
            if (null != open) {
                try {
                    open.close();
                    open = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            Log.d("解析的数据", "解析完毕");
        }
        return "";
    }

}
